package com.epam.giorgadze.task1.entity.plane_type;

import com.epam.giorgadze.task1.entity.PlaneModel;

import java.util.Comparator;

/**
 * /**
 * одно предложение о сути класса. (точка обязательна)
 * тут может быть какое-то уточнение, описание
 *
 * @author имя, ну и также другие теги через @, о них не буду
 *         перед методами и полями
 *         /**
 *         суть.
 *         описание
 * @parfam параметр
 * @retfurn параметр
 */


public abstract class Plane {
    private PlaneModel model;
    private double travelingDistance;
    private double fuelConsumption;
    private double price;
    private double lifespan;

    public boolean checkTravelingDistance(double d) {
        if (d >= 0) {
            return true;
        } else return false;
    }

    public boolean checkFuelConsumption(double d) {
        if (d >= 0) {
            return true;
        } else return false;
    }

    public boolean checkPrice(double d) {
        if (d >= 0) {
            return true;
        } else return false;
    }

    public boolean checkLifespan(double d) {
        if (d >= 0) {
            return true;
        } else return false;
    }

    public Plane() {
    }

    public Plane(PlaneModel model, double travelingDistance, double fuelConsumption, double price, double lifespan) {
        this.model = model;
        if (checkTravelingDistance(travelingDistance)) this.travelingDistance = travelingDistance;
        if (checkFuelConsumption(fuelConsumption)) this.fuelConsumption = fuelConsumption;
        if (checkPrice(price)) this.price = price;
        if (checkLifespan(lifespan)) this.lifespan = lifespan;
    }

    public double getFuelConsumption() {
        return fuelConsumption;
    }

    public void setFuelConsumption(double fuelConsumption) {
        if (checkFuelConsumption(fuelConsumption)) this.fuelConsumption = fuelConsumption;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        if (checkPrice(price)) this.price = price;
    }

    public double getLifespan() {
        return lifespan;
    }

    public void setLifespan(double lifespan) {
        if (checkLifespan(lifespan)) this.lifespan = lifespan;
        else this.lifespan = 0;
    }

    public PlaneModel getModel() {
        return model;
    }

    public void setModel(PlaneModel model) {
        this.model = model;
    }

    public double getTravelingDistance() {
        return travelingDistance;
    }

    public void setTravelingDistance(double travelingDistance) {
        if (checkTravelingDistance(travelingDistance)) this.travelingDistance = travelingDistance;
    }

    @Override
    public String toString() {
        return "Plane " +
                "model: " + model +
                ", traveling distance = " +  String.format("%8.3f", this.getTravelingDistance()) +
                ", fuel consumption = " +  String.format("%8.3f", this.getFuelConsumption()) +
                ", price = " +  String.format("%8.3f", this.getPrice()) +
                ", lifespan = " +  String.format("%8.3f", this.getLifespan());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Plane plane = (Plane) o;

        if (Double.compare(plane.fuelConsumption, fuelConsumption) != 0) return false;
        if (Double.compare(plane.lifespan, lifespan) != 0) return false;
        if (Double.compare(plane.price, price) != 0) return false;
        if (Double.compare(plane.travelingDistance, travelingDistance) != 0) return false;
        if (model != plane.model) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = model != null ? model.hashCode() : 0;
        temp = Double.doubleToLongBits(travelingDistance);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(fuelConsumption);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(price);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(lifespan);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    public static class TravelingDistanceComparator implements Comparator<Plane> {
        public static final Comparator<Plane> TRAVELING_DISTANCE_ORDER = new TravelingDistanceComparator();

        public int compare(Plane p1, Plane p2) {
            return Double.compare(p1.travelingDistance, p2.travelingDistance);
        }
    }



}

